function refreshPage() {
    /// <summary>
    /// 强制刷新当前页
    /// </summary>
    /// <returns type=""></returns>
    location.reload(true);
}
$(function () {
    //删除
    $(document).on('click',
        '.delete',
        function () {
            var result = confirm("确认要删除？");
            if (result === true) {
                //删除收件地址
                var $li = $(this).parent().parent();
                var receiptId = $li.data('receiptid');
                var param = {
                    receiptId: receiptId
                }
                $.post(ReceiptUrl.DeleteReceipt, param, function (data) {
                    alert(data.Message);
                    if (data.Success) {
                        refreshPage();
                        $li.remove();
                    }
                });
            } else {
                return false;
            }
        });
    //设置默认
    $(function () {
        $(document).on('click',
            '.default',
            function () {
                /// <summary>
                /// 设置默认收件信心
                /// </summary>
                var $li = $(this).parent().parent();
                var receiptId = $li.data('receiptid');
                var param = {
                    receiptId: receiptId
                }
                $.post(ReceiptUrl.SetDefault,
                    param,
                    function (data) {
                        alert(data.Message);
                        refreshPage();
                        //$li.addClass('ck').siblings().removeClass('ck');
                    });
            });
    });
    //省份改变事件
    $(document).on('change',
        '#ProvinceId',
        function () {
            var province = $("#ProvinceId").val();
            var param = { parentId: province };
            $.post(ReceiptUrl.AreaList,
                param,
                function (data) {
                    $("#CityId").html('<option value="">请选择市...</option>' + data);
                    $("#DistrictsId").html('<option value="">请选择区/县...</option>');
                });
        });
    //市区改变事件
    $(document).on('change',
        '#CityId',
        function () {
            var city = $("#CityId").val();
            var param = { parentId: city };
            $.post(ReceiptUrl.AreaList,
                param,
                function (data) {
                    $("#DistrictsId").html('<option value="">请选择区/县...</option>' + data);
                });
        });
    //添加弹窗
    $(document).on('click',
        '#btnAddInvoice',
        function () {
            $.post(ReceiptUrl.AddOrEdit,
                null,
                function (data) {
                    $("#editor-template").html(data);
                });
        });
    //修改弹窗
    $(document).on('click',
        '.modify',
        function () {
            var $li = $(this).parent().parent();
            var receiptId = $li.data('receiptid');
            var param = { receiptId: receiptId };
            $.post(ReceiptUrl.AddOrEdit,
                param,
                function (data) {
                    $("#editor-template").html(data);
                    var h_Province = $("#h_Province").val();
                    $("#ProvinceId option[value='" + h_Province + "']").prop("selected", true);
                    var h_City = $("#h_City").val();
                    $("#CityId option[value='" + h_City + "']").prop("selected", true);
                    var h_PostDistrict = $("#h_PostDistrict").val();
                    $("#DistrictsId option[value='" + h_PostDistrict + "']").prop("selected", true);
                });
        });
    $(document).on('click',
        '.btn-save',
        function () {
            var name = $("#Name").val();
            var phoneNumber = $("#PhoneNumber").val();
            var provinceId = $("#ProvinceId").val();
            var cityId = $("#CityId").val();
            var districtsId = $("#DistrictsId").val();
            var detailedAddress = $("#DetailedAddress").val();
            if (name === "" || name == null) {
                alert("请输入收件人");
                return false;
            }
            if (phoneNumber === "" || phoneNumber == null) {
                alert("请输入手机号");
                return false;
            }
            if (provinceId.length < 1) {
                alert("请选择省份");
                return false;
            }
            if (cityId.length < 1) {
                alert("请选择市区");
                return false;
            }
            if (districtsId.length < 1) {
                alert("请选择区/县");
                return false;
            }
            if (detailedAddress === "" || detailedAddress == null) {
                alert("请输入详细地址");
                return false;
            }
            var receiptId = $("#ReceiptId").val();
            var param = $("#ReceiptForm").serialize();
            if (receiptId === "" || receiptId == null) {
                //添加
                $.post(ReceiptUrl.AddReceiptAddress,
                    param,
                    function (data) {
                        alert(data.Message);
                        refreshPage();
                    });
            } else {
                //修改
                $.post(ReceiptUrl.SaveReceiptAddress,
                    param,
                    function (data) {
                        alert(data.Message);
                        refreshPage();
                    });
            }
        });
    //关闭编辑面板
    $(document).on('click', '.btn-close', function () {
        $("#editor-template").html('');
    });
});